package org.testis.gui.mvc.controller.dynamicmap;

import org.testis.gui.mvc.controller.*;
import java.util.LinkedList;

import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;

import org.testis.utils.tree.MyDefaultMutableTreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class DynamicMapPopupController extends AbstractController {

    public static final String NODE_EDIT = "DynamicMapEdit";
    public static final String NODE_DEPENDENCY = "DynamicMapDependency";
    public static final String NODE = "ClonedObject";
    public static final String SET_POPUP_MENU_CONTAINER_VISIBILITY =
            "DynamicMapContainerVisibility";
    public static final String SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY =
            "DynamicMapObjectGroupVisibility";
    public static final String SET_POPUP_MENU_OBJECT_VISIBILITY =
            "DynamicMapObjectVisibility";
    public static final String SET_POPUP_MENU_BEHAVIOUR_VISIBILITY =
            "DynamicMapBehaviourVisibility";
    public static final String SET_POPUP_MENU_DELETE_VISIBILITY =
            "DynamicMapDeleteVisibility";
    public static final String SET_POPUP_MENU_EDIT_VISIBILITY =
            "DynamicMapEditVisibility";
    public static final String SET_POPUP_MENU_CLONE_VISIBILITY =
            "DynamicMapCloneVisibility";
    public static final String SET_POPUP_MENU_COPY_VISIBILITY =
            "DynamicMapCopyVisibility";
    public static final String SET_POPUP_MENU_1_TO_1_COPY_VISIBILITY =
            "DynamicMap1To1CopyVisibility";
    public static final String SET_POPUP_MENU_PASTE_CLONE_VISIBILITY =
            "DynamicMapPasteVisibility";
    public static final String SET_POPUP_MENU_SEARCH_ABSID_TREE_VISIBILITY =
            "DynamicMapSearchAbstractIdVisibility";
    public static final String SET_POPUP_MENU_CLOSE_TREE_VISIBILITY =
            "DynamicMapCloseTreeVisibility";
    public static final String SET_POPUP_MENU_COPY_SUITE_ATTRIBUTES_VISIBILITY =
            "DynamicMapCopySuiteAttributesVisibility";
    public static final String CHANGE_PASTE_LABEL = "DynamicMapPasteLabel";
    public static final String OPEN_POPUP_MENU = "OpenDynamicMapMenu";
    public static String SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY = "DynamicMapValidationVisibility";

    public DynamicMapPopupController() {
        super();
    }

    public void operationChangePasteLabel(String label) {
        setModelProperty(CHANGE_PASTE_LABEL,
                label);
    }

    public void operationSetNode(MyDefaultMutableTreeNode node,
            String label) {
        setModelProperty(NODE,
                (DynamicMapDesignTreeNode) node.getUserObject());
        operationChangePasteLabel(label);
    }

    public DynamicMapDesignTreeNode operationGetNode() {
        /*DynamicMapDesignTreeNode node=(DynamicMapDesignTreeNode) getModelProperty(NODE);
        if(node==null)
        return null;
        if(node.getObject()==null)
        return null;
        return node;*/
        /* return (DynamicMapDesignTreeNode)DynamicMapControllerList.newInstance().
        getDynamicMapTreeController().
        operationGetSelectedNode().getUserObject();
         */
        return (DynamicMapDesignTreeNode) getModelProperty(NODE);
    }

    public void operationOpenPopupMenu(MyDefaultMutableTreeNode node,
            LinkedList coordinates) {
        Object obj = node.getUserObject();
        if (obj != null) {
            String groupName = "";
            String typeName = "";
            Integer id = 0;
            if (obj instanceof DynamicMapDesignTreeNode) {
                groupName =
                        ((DynamicMapDesignTreeNode) obj).getObject().getGroupName();
                typeName = ((DynamicMapDesignTreeNode) obj).getObject().getTypeName();
                id = ((DynamicMapDesignTreeNode) obj).getObject().getId();
            } else {
                groupName = obj.toString();
            }

            if (groupName.equals("Container")) {
                setModelProperty(SET_POPUP_MENU_CONTAINER_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_OBJECT_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_BEHAVIOUR_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_DELETE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_EDIT_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_CLONE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_COPY_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_1_TO_1_COPY_VISIBILITY,
                        true);
                if (operationGetNode() != null &&
                        operationGetNode().getObject() != null &&
                        ((operationGetNode().getObject().getGroupName().equals(
                        "Object")) ||
                        (operationGetNode().getObject().getGroupName().equals(
                        "ObjectGroup")) ||
                        (operationGetNode().getObject().getGroupName().equals(
                        "Container")) ||
                        (operationGetNode().getObject().getGroupName().equals(
                        "Behaviour")))) {
                    setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                            true);
                } else {
                    setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                            false);
                }
                setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_COPY_SUITE_ATTRIBUTES_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY,
                        true);
                if (typeName.equals("UIDeviceMapContainer")) {
                    setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                            true);
                    setModelProperty(SET_POPUP_MENU_COPY_SUITE_ATTRIBUTES_VISIBILITY,
                            true);
                    setModelProperty(SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY,
                            true);
                    if (operationGetNode() == null) {
                        setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                                false);
                    } else if (operationGetNode().getObject().getTypeName().equals("UIDeviceMapContainer") && !typeName.equals("UIDeviceMapContainer")) {
                        setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                                false);
                    } else if (operationGetNode().getObject().getId().equals(id)) {
                        setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                                false);
                    }
                }
            } else if (groupName.equals("ObjectGroup")) {
                setModelProperty(SET_POPUP_MENU_CONTAINER_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY,
                        false);
                if (typeName.equals("UIDynamicGrid")) {
                    setModelProperty(SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY,
                            true);
                }
                setModelProperty(SET_POPUP_MENU_OBJECT_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_BEHAVIOUR_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_DELETE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_EDIT_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_CLONE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_COPY_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_1_TO_1_COPY_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_COPY_SUITE_ATTRIBUTES_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY,
                        true);
                if (operationGetNode() != null &&
                        ((operationGetNode().getObject().getGroupName().equals(
                        "Object")) ||
                        (operationGetNode().getObject().getGroupName().equals(
                        "ObjectGroup")) ||
                        (operationGetNode().getObject().getGroupName().equals(
                        "Behaviour")))) {
                    setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                            true);
                } else {
                    setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                            false);
                }
                setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                        false);
            } else if (groupName.equals("Object")) {
                setModelProperty(SET_POPUP_MENU_CONTAINER_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_OBJECT_VISIBILITY,
                        false);

                setModelProperty(SET_POPUP_MENU_BEHAVIOUR_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_DELETE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_EDIT_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_CLONE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_COPY_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_1_TO_1_COPY_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_COPY_SUITE_ATTRIBUTES_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY,
                        true);
                if (operationGetNode() != null &&
                        (operationGetNode().getObject().getGroupName().equals(
                        "Behaviour"))) {
                    setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                            true);
                } else {
                    setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                            false);
                }
                setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                        false);
            } else if (groupName.equals("Behaviour")) {
                setModelProperty(SET_POPUP_MENU_CONTAINER_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_OBJECT_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_BEHAVIOUR_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_DELETE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_EDIT_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_CLONE_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_COPY_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_1_TO_1_COPY_VISIBILITY,
                        true);
                setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_COPY_SUITE_ATTRIBUTES_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_VALIDATE_TREE_VISIBILITY,
                        true);
            } else {
                setModelProperty(SET_POPUP_MENU_CONTAINER_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_OBJECT_GROUP_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_OBJECT_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_BEHAVIOUR_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_DELETE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_EDIT_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_CLONE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_COPY_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_1_TO_1_COPY_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_PASTE_CLONE_VISIBILITY,
                        false);
                setModelProperty(SET_POPUP_MENU_CLOSE_TREE_VISIBILITY,
                        false);
            }
            setModelProperty(SET_POPUP_MENU_SEARCH_ABSID_TREE_VISIBILITY,
                    true);
            setModelProperty(OPEN_POPUP_MENU,
                    coordinates);
        }
    }
}
